package com.citusdata.elven.router;

import java.lang.Comparable;


public class Endpoint implements Comparable {
  private final String hostname;
  private final int    port;
  private final String zone; 

  public Endpoint(String hostname, int port) {
    this.hostname = hostname;
    this.port  = port;
    this.zone  = "";
  }

  public Endpoint(String hostname, int port, String zone) {
    this.hostname = hostname;
    this.port  = port;
    this.zone  = zone;
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof Endpoint)) {
      return false;
    }
    Endpoint ep = (Endpoint) obj;
    return (ep.zone.equals(zone) && ep.hostname.equals(hostname) &&
            ep.port == port);
  }

  public int compareTo(Object obj) {
    Endpoint ep = (Endpoint) obj;

    int zoneDiff = zone.compareTo(ep.zone);
    if (zoneDiff != 0) {
      return zoneDiff;
    }

    int hostnameDiff = hostname.compareTo(ep.hostname);
    if (hostnameDiff != 0) {
      return hostnameDiff;
    }

    return (port - ep.port);
  }

  public int hashCode() {
    int result = 17;
    result = 37*result + zone.hashCode();
    result = 37*result + hostname.hashCode();
    result = 37*result + port;
    return result;
  }

  public String toString() {
    String fullName = hostname + ":" + port;
    return fullName;
  }

  public String getHostname() {
    return hostname;
  }

  public int getPort() {
    return port;
  }

  public String getZone() {
    return zone;
  }
}
